package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //目的地
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //作者
        //摘要
        //1.攻略文章作者自己提取（推荐
        //2.截取，文章内容前200字符
        String content = strategyDetail.getStrategyContent().getContent();
        if(content.length()>200){
            strategyDetail.setSummary(content.substring(0,200));
        }else {
            strategyDetail.setSummary(content);
        }
        //是否国外
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        strategyDetail.setIsabroad(toasts.get(0).getId() != 1l);//中国是1l
        //内容
        if(strategyDetail.getId() == null){
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            strategyDetail.getStrategyContent().setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyDetail.getStrategyContent());
        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyDetail.getStrategyContent().setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyDetail.getStrategyContent());
        }

        //标签
        strategyDetailMapper.deleteDetail_Tag_RelationByDetailId(strategyDetail.getId());
        String[] tagsArr = StringUtils.split(tags, ',');
        StrategyTag strategyTag = new StrategyTag();
        for (String tagStr : tagsArr) {
            strategyTag.setName(tagStr);
            Long tagId = strategyTagService.save(strategyTag);
            strategyDetailMapper.insertDetail_Tag_Relation(strategyDetail.getId(),tagId);
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteDetail(Long id) {
        strategyDetailMapper.deleteDetail_Tag_RelationByDetailId(id);
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> queryStrategyTop3ByDestId(Long id) {
        return strategyDetailMapper.selectStrategyTop3ByDestId(id);
    }

    @Override
    public void updateStatisVO(StatisVO pvo) {
        strategyDetailMapper.updateStatisVO(pvo);
    }


}
